from captcha.image import ImageCaptcha
import io
import random
import string

# 生成随机验证码
def generate_captcha_text():
    captcha_text = ''.join(random.choices(string.ascii_letters +
                                          string.digits, k=4))
    return captcha_text

# 生成验证码图片
def generate_captcha_image(text):
    image = ImageCaptcha()
    data = image.generate_image(text)

    img_byte_array = io.BytesIO()
    data.save(img_byte_array, format='PNG')
    binary_image = img_byte_array.getvalue()
    return binary_image

